<template>
  <el-card>
    <div slot="header" style="line-height: 40px;">
      <h1>添加学生</h1>
    </div>
    <el-form ref="studentsForm" :rules="rules" label-width="80px" :model="studentsForm">
      <el-form-item label="ID" v-show="false">
        <el-input v-model="studentsForm.id"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="code">
        <el-input v-model.trim="studentsForm.code"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="sname">
        <el-input v-model.trim="studentsForm.sname"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="studentsForm.gender">
          <el-radio :label="0">女</el-radio>
          <el-radio :label="1">男</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model.trim="studentsForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="宿舍楼" prop="dormId">
        <el-select v-model.trim="studentsForm.dormId" clearable placeholder="请选择">
          <el-option v-for="item in $store.state.dorms" :key="item.id" :label="item.cname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="寝室号" prop="roomId">
        <el-input v-model.trim="studentsForm.roomId"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="studentsForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import studentsApi from "../http/studentsApi";

export default {
  name: "Add",
  data() {
    let checkCode = (rule, value, callback) => {
      studentsApi.checkCode(value,this.studentsForm.id).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          if (res.data.msg){
            callback(new Error());
          }else{
            callback();
          }
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'error'
          });
        }
      })
    };

    return {
      studentsForm: {},
      rules: {
        code: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'},
          {validator:checkCode, message: '学号不能重复', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        sname: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        gender: [
          {required: true, message: '请选择性别', trigger: 'change'},
        ],
        dormId: [
          {required: true, message: '请选择宿舍楼', trigger: 'change'},
        ],
        roomId: [
          {required: true, message: '请选择寝室号', trigger: 'change'},
        ],
        phone: [
          {required: true, message: '请输入手机', trigger: 'blur'},
          {pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '必须是正确的手机号码', trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['studentsForm'].validate((valid) => {
        if (valid) {
          console.log('submit!', this.studentsForm);
          studentsApi.save(this.studentsForm).then((res) => {
            console.log(res)
            if (res.data.code == 200) {
              this.$router.replace("/students.html")
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;//阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs["studentsForm"].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        studentsApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.studentsForm = res.data.data;
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>